package com.gxa.jjty.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.jjty.admin.mapper.HandicappedMapper;
import com.gxa.jjty.admin.mapper.OprRecordMapper;
import com.gxa.jjty.admin.mapper.OrderoRecordMapper;
import com.gxa.jjty.admin.service.HandicappedService;
import com.gxa.jjty.admin.service.TorderService;
import com.gxa.jjty.core.pojo.*;
import com.gxa.jjty.core.service.impl.BaseServiceImpl;
import com.gxa.jjty.core.util.JsonUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author wanjun
 * @version 1.0
 * @date 2021/12/1 20:20
 */
@Service("torderService")
@Transactional(rollbackFor = Throwable.class)
public class TorderServiceImpl extends BaseServiceImpl<Torder> implements TorderService {


    @Autowired
    Mapper<Torder> mapper = null;
    @Autowired
    Mapper<PhyUser> phyUserMapper = null;
    @Autowired
    HandicappedService handicappedService;
    @Autowired
    HandicappedMapper handicappedMapper;
    @Autowired
    OrderoRecordMapper orderoRecordMapper;

    @Override
    public PageInfo<Torder> selectOrderByField(Date from, Date to, Torder torder, Integer page,Integer size) throws IllegalAccessException {
        PageHelper.startPage(page,size);
        Example example = new Example(torder.getClass());
        Example.Criteria criteria = example.createCriteria();
        Class<?> cls = torder.getClass();
        Field[] declaredFields = cls.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            String name = declaredField.getName();
            Object o = declaredField.get(torder);
            if (o != null){
                criteria.orLike(name,o.toString());
            }
        }
        if(from!=null&&to!=null){
            criteria.andBetween("orderTime",from,to);
        }
        example.orderBy("orderId").desc();
        List<Torder> torders = mapper.selectByExample(example);


        for (Torder torder1 : torders) {

            //将操作记录装在订单对象里边
            OrderoRecord orderoRecord = new OrderoRecord();
            orderoRecord.setOrderId(torder1.getOrderId());
            List<OrderoRecord> recordList = orderoRecordMapper.select(orderoRecord);
            torder1.setRecordList(recordList);

            if(torder1.getPhyId()!=null){
                //通过陪护员id拿到陪护员电话,陪护员姓名,并存到返回的对象中
                PhyUser phyUser = phyUserMapper.selectByPrimaryKey(torder1.getPhyId());
                if(phyUser!=null){
                    torder1.setPhyPhone(phyUser.getPhone());
                    torder1.setPhyName(phyUser.getName());
                }
            }

            if(torder1.getPhyId()!=null){
                //通过残疾人id拿到残疾人电话,姓名,并存到返回的对象中,避免因数据修改,订单表中数据不同步问题
                Handicapped handicapped = handicappedService.selectById(torder1.getCjrId());
                if(handicapped!=null){
                    torder1.setOrderUser(handicapped.getName());
                    torder1.setOrderPhone(handicapped.getPhone());
                }
            }
        }


        PageInfo<Torder> of = PageInfo.of(torders);
        return of;
    }



    //查询全部订单,逆序,分页
    @Override
    public PageInfo<Torder> selectAllOrder(Integer page, Integer size) throws Exception{
        PageHelper.startPage(page,size);
        Example example=new Example(Torder.class);
            //实现逆序
            example.orderBy("orderId").desc();
        List<Torder> torders = mapper.selectByExample(example);


        for (Torder torder1 : torders) {

            //将操作记录装在订单对象里边
            OrderoRecord orderoRecord = new OrderoRecord();
            orderoRecord.setOrderId(torder1.getOrderId());
            List<OrderoRecord> recordList = orderoRecordMapper.select(orderoRecord);
            torder1.setRecordList(recordList);

            if(torder1.getPhyId()!=null){
                //通过陪护员id拿到陪护员电话,陪护员姓名,并存到返回的对象中
                PhyUser phyUser = phyUserMapper.selectByPrimaryKey(torder1.getPhyId());
                if(phyUser!=null){
                    torder1.setPhyPhone(phyUser.getPhone());
                    torder1.setPhyName(phyUser.getName());
                }
            }

            if(torder1.getPhyId()!=null){
                //通过残疾人id拿到残疾人电话,姓名,并存到返回的对象中,避免因数据修改,订单表中数据不同步问题
                Handicapped handicapped = handicappedService.selectById(torder1.getCjrId());
                if(handicapped!=null){
                    torder1.setOrderUser(handicapped.getName());
                    torder1.setOrderPhone(handicapped.getPhone());
                }
            }
        }

        PageInfo<Torder> list = PageInfo.of(torders);
        return list;
    }

    @Override
    public void updateByIdAndRecord(Torder torder,OrderoRecord orderoRecord) throws IllegalAccessException {
        orderoRecordMapper.insertSelective(orderoRecord);
        super.updateById(torder);
    }

    @Override
    public List<Object> getOrderDetail(String orderNumber) {
        //时间格式处理
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Torder torder = new Torder();
        torder.setOrderNumber(orderNumber);
        //torder.setOrderNumber("CJR20211214162204175505");
        Torder torder1 = mapper.select(torder).get(0);
        //残疾人
        Handicapped handicapped = handicappedMapper.selectByPrimaryKey(torder1.getCjrId());
        //陪护员
        PhyUser phyUser = phyUserMapper.selectByPrimaryKey(torder1.getPhyId());
        //操作记录
        OrderoRecord orderoRecord = new OrderoRecord();
        orderoRecord.setOrderId(torder1.getOrderId());
        List<OrderoRecord> orderoRecords = orderoRecordMapper.select(orderoRecord);

        //订单信息
        Map<String, Object> orderDetail = new HashMap<>();
        orderDetail.put("orderId",torder1.getOrderId());
        orderDetail.put("orderNumber",torder1.getOrderNumber());
        orderDetail.put("orderTime",format.format(torder1.getOrderTime()));
        if(torder1.getSerialNumber()==null){
            orderDetail.put("serialNumber","-");
        }else {
            orderDetail.put("serialNumber",torder1.getSerialNumber());
        }

        orderDetail.put("user",handicapped.getName());
        orderDetail.put("payType",torder1.getPayType());

        //费用信息
        Map<String, Object> payDetail = new HashMap<>();
        payDetail.put("serveType",torder1.getServeType());
        payDetail.put("price",torder1.getOrderPrice());
        if(torder1.getPayTime()!=null){
            payDetail.put("state","已支付");
            payDetail.put("subtotal",torder1.getPayPrice());
            if("免支付".equals(torder1.getPayType())){
                payDetail.put("payable","￥0");
            }else {
                payDetail.put("payable","￥"+torder1.getOrderPrice());
            }
        }else {
            payDetail.put("state","未支付");
            payDetail.put("subtotal","-");
            payDetail.put("payable",torder1.getOrderPrice());
        }

        //服务信息
        Map<String, Object> serveDetail = new HashMap<>();
        serveDetail.put("user",handicapped.getName());
        serveDetail.put("phone",handicapped.getPhone());
        serveDetail.put("orderAddress",torder.getOrderAddress());
        if(torder1.getBookTime()==null){
            serveDetail.put("bookTime","立即服务");
        }else {
            serveDetail.put("bookTime",format.format(torder1.getBookTime()));
        }
        serveDetail.put("orderRemark",torder1.getOrderRemark());

        //陪护员信息
        Map<String, Object> phyDetail = new HashMap<>();
        if(phyUser==null){
            phyDetail.put("phy","-");
            phyDetail.put("phone","-");
        }else {
            phyDetail.put("phy",phyUser.getName());
            phyDetail.put("phone",phyUser.getPhone());
        }
        phyDetail.put("CSD","-");
        if(torder1.getServeTime()==null){
            phyDetail.put("serveTime","-");
        }else {
            phyDetail.put("serveTime",format.format(torder1.getServeTime()));
        }
        if(torder1.getServfinishTime()==null){
            phyDetail.put("servfinishTime","-");
        }else {
            phyDetail.put("servfinishTime",format.format(torder1.getServfinishTime()));
        }

        //操作记录
        Map<String, Object> oprerateDetail = new HashMap<>();
        oprerateDetail.put("orderoRecord",orderoRecords);
        //将几个Map放到一个list中
        ArrayList<Object> list = new ArrayList<>();
        list.add(orderDetail);
        list.add(payDetail);
        list.add(serveDetail);
        list.add(phyDetail);
        list.add(orderoRecords);
        return list;


    }
}
